package com.jy.datapipeline.export.dao.impl;

import com.jy.datapipeline.export.entity.DataSourceInfoModel;
import com.jy.datapipeline.export.dao.DataSourceInfoDao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mysql.cj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

@Slf4j
@Repository
public class DataSourceInfoDaoImpl implements DataSourceInfoDao {
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Override
    public List<Map<String, Object>> findDataSourceInfoByList(String dataSourceName,
        int iDisplayStart, int iDisplayLength) {
        List<Map<String, Object>> list=new ArrayList<>();
        try {
            StringBuffer sb=new StringBuffer();
            Map<String,Object> params = new HashMap<String,Object>();
            sb.append("select a.id,a.data_source_id,a.data_source_name,a.jdbc_url,a.password,a.username,DATE_FORMAT(a.update_time,'%Y-%m-%d %H:%i:%s') as update_time  from data_source_info a where 1=1 ");
            if(dataSourceName!=null && !dataSourceName.equals("")){
                sb.append(" and a.data_source_name like :dataSourceName");
                params.put("dataSourceName","%"+dataSourceName+"%");
            }

            sb.append(" order by a.update_time desc  ");
            if(iDisplayStart>-1 && iDisplayLength>-1){
                sb.append("LIMIT  :iDisplayStart ,:iDisplayLength ;");
                params.put("iDisplayStart",iDisplayStart);
                params.put("iDisplayLength",iDisplayLength);
            }

            list=this.namedParameterJdbcTemplate.queryForList(sb.toString(),params);


        }catch (Exception e){
            log.error("findDataSourceInfoByList exception",e);
        }

        return list;
    }

    @Override
    public int findDataSourceInfoByCount(String dataSourceName) {
        int count=0;
        List<Map<String, Object>> list=new ArrayList<>();
        try {
            StringBuffer sb=new StringBuffer();
            Map<String,Object> params = new HashMap<String,Object>();
            sb.append("select COUNT(a.id) as count from data_source_info a where 1=1 ");
            if(dataSourceName!=null && !dataSourceName.equals("")){
                sb.append(" and a.data_source_name like :dataSourceName");
                params.put("dataSourceName","%"+dataSourceName+"%");
            }
            list=this.namedParameterJdbcTemplate.queryForList(sb.toString(),params);
            if(list.size()>0){
                count=Integer.parseInt(list.get(0).get("count")!=null?list.get(0).get("count").toString():"0");
            }
        }catch (Exception e){
            log.error("findDataSourceInfoByCount exception",e);
        }

        return count;
    }

    @Override
    public boolean updateDataSourceInfo(DataSourceInfoModel dsim) {
        Map<String,Object> params = new HashMap<String,Object>();
        StringBuilder sb=new StringBuilder();
        sb.append("update data_source_info set update_time=NOW()");
        if (!StringUtils.isNullOrEmpty(dsim.getDataSourceName())) {
            sb.append(" ,data_source_name=:dataSourceName");
            params.put("dataSourceName",dsim.getDataSourceName());
        }
        if (!StringUtils.isNullOrEmpty(dsim.getJdbcUrl())) {
            sb.append(" ,jdbc_url=:jdbcUrl");
            params.put("jdbcUrl",dsim.getJdbcUrl());
        }
        if (!StringUtils.isNullOrEmpty(dsim.getUserName())) {
            sb.append(" ,username=:username");
            params.put("username",dsim.getUserName());
        }
        if (!StringUtils.isNullOrEmpty(dsim.getPassword())) {
            sb.append(" ,password=:password");
            params.put("password",dsim.getPassword());
        }
        sb.append(" where data_source_id=:dataSourceId");
        params.put("dataSourceId", dsim.getDataSourceId());
        int req=this.namedParameterJdbcTemplate.update(sb.toString(),params);
        return req>0?true:false;
    }
}
